package com.aduerr.v1shouyin.mvp.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.MemberRechargeDetailEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.print.PrinterWrapper;
import com.aduerr.v1shouyin.unioncard.BaseUnionActivity;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/5/27 16:57
 * @change
 * @chang time
 * @class describe
 */

public class MemberRechargeDetailActivity extends BaseUnionActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.iv_icon_type)
    ImageView ivIconType;
    @BindView(R.id.tv_pay_state)
    TextView tvPayState;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_rmb_01)
    TextView tvRmb01;
    @BindView(R.id.tv_send_money)
    TextView tvSendMoney;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_order_id)
    TextView tvOrderId;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_transaction_id)
    TextView tvTransactionId;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_cashier)
    TextView tvCashier;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_print)
    TextView tvPrint;
    MemberRechargeDetailEntity.DataBean printBean;
    private String orderId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_recharge_detail;
    }

    @Override
    protected void initBase() {
        super.initView();
        ButterKnife.bind(this);
        orderId = getIntent().getStringExtra("orderid");
        tvTitle.setText("充值详情");
        getOrderDetail(orderId);
    }

    private void getOrderDetail(String orderId) {

        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("orderid", orderId);
        APIRetrofit.getAPIService().getMemeberRechargeDetail(RXRequest.getXingPosParams(addParams
                , this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(entity -> {
                    if(entity.getSuccess() == 1) {
                        MemberRechargeDetailEntity.DataBean dataBean = entity.getData();
                        printBean = entity.getData();
                        if(dataBean.getPayType() == 0) {
                            Glide.with(this).load(R.drawable.ic_unionpay).into(ivIconType);
                            tvPayType.setText("现金");
                        } else if(dataBean.getPayType() == 1) {
                            Glide.with(this).load(R.drawable.ic_wechatpay).into(ivIconType);
                            tvPayType.setText("微信");
                        } else if(dataBean.getPayType() == 2) {
                            Glide.with(this).load(R.drawable.ic_alipay).into(ivIconType);
                            tvPayType.setText("支付宝");
                        } else if(dataBean.getPayType() == 3) {
                            Glide.with(this).load(R.drawable.ic_yunshanfu).into(ivIconType);
                            tvPayType.setText("云闪付");
                        }
                        tvMoney.setText(dataBean.getRechargeMoney() + "");
                        tvSendMoney.setText(dataBean.getGiveMoney() + "");
                        tvOrderId.setText(dataBean.getOrderId());
                        tvTransactionId.setText(dataBean.getOutOrderId());
                        tvPayTime.setText(dataBean.getPayTime());
                        tvShopName.setText(dataBean.getShopName());
                        tvCashier.setText(dataBean.getSiteUserName());
                        tvRemark.setText(dataBean.getRemark());
                    } else {
                        ToastUtils.showToast(this, entity.getErrMsg());
                        finish();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    ToastUtils.showToast(this, "网络异常");
                    finish();
                });

    }

    @OnClick({R.id.img_back, R.id.tv_print})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_print:
                if(printBean == null) return;
                printText(printBean);
                break;
        }
    }

    public void printText(MemberRechargeDetailEntity.DataBean dataBean) {

        int printpage = (int) SharedPreferencesUtils.getParam(this, "printpage", 1);
        if(printpage == 0) {
            ToastUtils.showToast(this, "打印张数设置为不打印,请先设置打印张数!!");
            return;
        }
        if(dataBean != null) {
            PrinterWrapper.printNewMemberRecharge(printpage, dataBean, false);
        }

    }


}
